package edu.xlh.travel.controller;

import com.alibaba.fastjson.JSONObject;
import edu.xlh.travel.common.Result;
import edu.xlh.travel.dao.CarouselDao;
import edu.xlh.travel.pojo.User;
import edu.xlh.travel.pojo.WeChatUser_phoneUser;
import edu.xlh.travel.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
;

@Controller
@RequestMapping(value = "/dist")
public class IndexController {
    @Autowired
    private ScenicService scenicService;

    @Autowired
    private HotelService hotelService;

    @Autowired
    private GalleryService galleryService;

    @Autowired
    private UserService userService;

    @Autowired
    private WeChatUser_phoneUserService weChatUser_phoneUserService;

    @Autowired
    private CarouselDao carouselDao;



    /*
    * to Index page
    * */
    @RequestMapping("/index")
    public String toIndex(Model model) {
        model.addAttribute("carousels" , carouselDao.findAll());
        return "page/index";
    }

    /*
    * to XingYingTravel page
    * */
    @RequestMapping("/travel")
    public String travel(Model model, @RequestParam(value = "start" ,defaultValue = "0")Integer start,
                         @RequestParam(value = "limit" ,defaultValue = "6")Integer limit,
                         @RequestParam(value = "price" ,defaultValue = "1")Integer price,
                         @RequestParam(value = "star" ,defaultValue = "1")Integer star) {


        return scenicService.findAllScenic(model , start , limit , price , star);
    }

    /*
    * to Galllery page
    * */
    @RequestMapping("/gallery")
    public String toGalllery(Model model) {
        return galleryService.findAllGallery(model);
    }

    /*
    * to Hotels page
    * */
    @RequestMapping("/hotels")
    public String toHotels(Model model, @RequestParam(value = "start" ,defaultValue = "0")Integer start,
                           @RequestParam(value = "limit" ,defaultValue = "6")Integer limit,
                           @RequestParam(value = "price" ,defaultValue = "1")Integer price,
                           @RequestParam(value = "star" ,defaultValue = "1")Integer star) {
        return hotelService.findAllHotel(model , start , limit , price , star);
    }

    /*
    * to Contacts page
    * */
    @RequestMapping("/contacts")
    public String toContacts() {
        return "page/contacts";
    }

    /*
    * to About page
    * */
    @RequestMapping("/about")
    public String toAbout() {
        return "page/about";
    }

    /*
    * user_login model
    * */

    /*
    * 1. to login page
    * */
    @RequestMapping("/login")
    public String login() {
        return "user/login";
    }

    /*
    * user_register model
    * */
    @RequestMapping("/register")
    public String register() {
        return "user/register";
    }

    /*
    * to login_email_forget page
    * */
    @RequestMapping("/forget")
    public String forget() {
        return "user/forget";
    }

    /*
     * 1. to admin_login page
     * */
    @RequestMapping("/adminLogin")
    public String adminLogin() {
        return "admin/login/adminlogin";
    }

    /*
    * to weCatLogin page
    * */
    @RequestMapping("/weCatLogin")
    public String weCatLogin() {
        return "/user/weCatLogin";
    }

    /*
    * to change personal information page
    * */
    @RequestMapping("/info")
    @ResponseBody
    public Result info(HttpServletRequest request) {
        return userService.getUserInfo(request);
    }

    @RequestMapping("/savaUserSession")
    @ResponseBody
    public Result savaUserSession(HttpSession session , HttpServletRequest request) {
        JSONObject weChatUser = (JSONObject) request.getServletContext().getAttribute("weChatUser");
        if (weChatUser != null) {
            WeChatUser_phoneUser weChatUser_phoneUserServiceByOpenId = weChatUser_phoneUserService
                    .findByOpenId((String)weChatUser.get("openid"));
            User user = userService.findById(weChatUser_phoneUserServiceByOpenId.getUserId()).get();
            session.setAttribute("user",user);
        }
        request.getServletContext().removeAttribute("weChatUser");
        return new Result(200 , "保存session成功");
    }

    @RequestMapping("/vipPayFor")
    public String vipPayFor () {
        return "user/vipPayFor";
    }

    @RequestMapping("/gogozhifuSusseccPage")
    public String gogozhifuSusseccPage () {
        return "/user/gogozhifuSusseccPage";
    }
}
